package com.gf.pf.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.gf.pf.model.User;
import com.gf.pf.model.UserJob;
import com.gf.pf.model.UserRole;

/**
 * 
 * @describe 用户
 * @date     2015年3月7日
 */
public interface UserMapper {
	
	/**
	 * 保存用户SQL  4QrcOUm6Wau+VuBX8g+IPg==  123456
	 */
	final String SAVE_USER = "INSERT INTO sys_user VALUES(#{id}, #{userLoginName}, '4QrcOUm6Wau+VuBX8g+IPg==', #{userName}, #{userPhone}, #{userTel}, #{userEmail}, #{userMemo}, ${userIsvalid}, #{userDepartmentId})";
	
	/**
	 * 删除用户SQL
	 */
	final String DELETE_BY_ID = "DELETE FROM sys_user WHERE user_id = #{userId}";

	/**
	 * 修改密码SQL
	 */
	final String UPDATE_PASSWORD = "UPDATE sys_user SET user_login_pwd = #{userLoginPwd} WHERE user_id = #{id}";
	
	/**
	 * 根据用户名、密码查询用户SQL
	 */
	final String GET_BY_USERNAME_PASSWORD = "SELECT user_id as id, user_login_name, user_login_pwd, user_name, user_phone, user_tel, user_email, user_memo, user_isvalid, user_department_id FROM sys_user WHERE user_isvalid = 1 and user_login_name = #{userName} AND user_login_pwd = #{password}";
	
	/**
	 * 查询部门下的所有用户SQL
	 */
	final String GET_BY_DEP_ID = "SELECT user_id as id, user_login_name, user_login_pwd, user_name, user_phone, user_tel, user_email, user_memo, user_isvalid FROM sys_user WHERE user_isvalid = 1 and use_department_id = #{depId}";
	
	/**
	 * 根据用户Id查询用户信息SQL
	 */
	final String GET_BY_USER_ID = "SELECT user_id as id, user_login_name, user_login_pwd, user_name, user_phone, user_tel, user_email, user_memo, user_isvalid, user_department_id FROM sys_user WHERE user_id = #{id}";
	
	/**
	 * 修改用户信息SQL
	 */
	final String MODIFY_BY_USER_ID = "UPDATE sys_user SET user_login_name = #{userLoginName}, user_name = #{userName}, user_phone = #{userPhone}, user_tel = #{userTel}, user_email = #{userEmail}, user_memo = #{userMemo}, user_isvalid = ${userIsvalid} WHERE user_id = #{id}";
	
	/**
	 * 根据用户id查询所属职位SQL
	 */
	final String JOB_BY_USER_ID = "SELECT job_id as jobId FROM sys_user_job WHERE user_id = #{userId}";
	
	/**
	 * 根据用户id删除所包含的职位SQL
	 */
	final String DELETE_JOB_BY_USER_ID = "DELETE sys_user_job WHERE user_id = #{userId}";
	
	/**
	 * 根据用户id保存所包含的职位SQL
	 */
	final String SAVE_JOB_BY_USER_ID = "INSERT INTO　sys_user_job VALUES(#{userId}, #{jobId})";
	
	/**
	 * 根据用户id查询所属角色SQL
	 */
	final String ROLE_BY_USER_ID = "SELECT role_id as roleId FROM sys_user_role WHERE user_id = #{userId}";
	
	/**
	 * 根据用户id删除所包含的角色SQL
	 */
	final String DELETE_ROLE_BY_USER_ID = "DELETE sys_user_role WHERE user_id = #{userId}";
	
	/**
	 * 根据用户id保存所包含的角色SQL
	 */
	final String SAVE_ROLE_BY_USER_ID = "INSERT INTO　sys_user_role VALUES(#{userId}, #{roleId})";
	
	/**
	 * 密码重置SQL
	 */
	final String RESET_BY_USER_ID = "UPDATE sys_user SET user_login_pwd = '4QrcOUm6Wau+VuBX8g+IPg==' WHERE user_id = #{id}";
	
	/**
	 * 
	 * @describe 根据用户名、密码查询用户
	 * @param map
	 * @return
	 */
	@Select(GET_BY_USERNAME_PASSWORD)
	User getUser(Map<String, Object> map);

	/**
	 * 
	 * @description 查询部门下的所有用户
	 * @return List<User>
	 */
	@Select(GET_BY_DEP_ID)
	List<User> getUserByDepId(String depId);
	
	/**
	 * 
	 * @describe 修改用户密码
	 * @param map
	 * @return
	 */
	@Update(UPDATE_PASSWORD)
	Integer modifyUserPassword(User user);
	
	/**
	 * 
	 * @describe 保存用户
	 * @param user
	 */
	@Insert(SAVE_USER)
	Integer saveUser(User user);
	
	/**
	 * 
	 * @description 根据用户Id查询用户信息
	 * @return User
	 */
	@Select(GET_BY_USER_ID)
	User getUserById(String id);
	
	/**
	 * 
	 * @description 修改用户信息
	 * @return Integer
	 */
	@Update(MODIFY_BY_USER_ID)
	Integer modifyUser(User user);
	
	/**
	 * 
	 * @description 删除用户
	 * @return Integer
	 */
	@Delete(DELETE_BY_ID)
	Integer deleteUser(String userId);
	
	/**
	 * 
	 * @description 根据用户id查询所属职位
	 * @return List<Map<String, String>>
	 */
	@Select(JOB_BY_USER_ID)
	List<Map<String, String>> jobByUserId(String userId);
	
	/**
	 * 
	 * @description 根据用户id删除所包含的职位
	 * @return Integer
	 */
	@Delete(DELETE_JOB_BY_USER_ID)
	Integer deleteJobByUser(String userId);
	
	/**
	 * 
	 * @description 根据用户id保存所包含的职位
	 * @return Integer
	 */
	@Insert(SAVE_JOB_BY_USER_ID)
	Integer saveJobByUser(UserJob uj);
	
	/**
	 * 
	 * @description 根据用户id查询所属角色
	 * @return List<Map<String, String>>
	 */
	@Select(ROLE_BY_USER_ID)
	List<Map<String, String>> roleByUserId(String userId);
	
	/**
	 * 
	 * @description 根据用户id删除所包含的角色
	 * @return Integer
	 */
	@Delete(DELETE_ROLE_BY_USER_ID)
	Integer deleteRoleByUser(String userId);
	
	/**
	 * 
	 * @description 根据用户id保存所包含的角色
	 * @return Integer
	 */
	@Insert(SAVE_ROLE_BY_USER_ID)
	Integer saveRoleByUser(UserRole ur);
	
	/**
	 * 
	 * @description 密码重置
	 * @return Integer
	 */
	@Update(RESET_BY_USER_ID)
	Integer resetUserPwd(User user);
}
